package edu.mse.camel.client.posterboard.ui.util;

import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.gef.tools.DeselectAllTracker;
import org.eclipse.swt.graphics.Color;

import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

import edu.mse.camel.client.log.Debug;
import edu.mse.camel.client.posterboard.ui.PosterboardPlugin;
import edu.mse.camel.client.posterboard.ui.editpart.AdaptedActivityDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.AdaptedClassDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.AdaptedSequenceDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.AdaptedStateMachineDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.AdaptedUsecaseDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.IPosterBoardDiagramEditpart;
import edu.mse.camel.client.posterboard.ui.editpart.PosterBoardEditpart;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;

public class EditpartUtil {
	
	static public class DiagramFigureUsingLocalCoordinates extends RectangleFigure {
		@Override
		protected boolean useLocalCoordinates(){
			return true;
		}
	};
	
	public static IFigure createFigure(Object model, ConnectionLayer connLayer) {
		Figure f = new DiagramFigureUsingLocalCoordinates(); //new RectangleFigure();
		f.setLayoutManager(new FreeformLayout());
		f.setBackgroundColor(new Color(null, 255, 255, 255));
		f.setOpaque(true);
		f.setBorder(new MarginBorder(5));
		if (model instanceof GraphNode) {
			f.setSize(((GraphNode) model).getSize());
		}
		try {
			connLayer.setConnectionRouter(new ShortestPathConnectionRouter(f));
		} catch (Exception e) {
			Debug.log_error(PosterboardPlugin.PLUGIN_ID, "The connection router failed because of the polyline");
		}
				
		return f;
	}
	
	public static void diagramSelectionChanged(GraphicalEditPart child) {
		PosterBoardEditpart pbe = (PosterBoardEditpart) child.getParent();
		if (child instanceof IPosterBoardDiagramEditpart) {
			pbe.diagramSelectionChanged(child, 
					((IPosterBoardDiagramEditpart) child).getType());
		}
	}
	
	public static DragTracker getDragTracker(Request req, GraphicalEditPart editPart){
		if ((req instanceof SelectionRequest) 
				&& (((SelectionRequest) req).getLastButtonPressed() == 3)) {
			return new DeselectAllTracker(editPart);
		}
		return new org.eclipse.gef.tools.DragEditPartsTracker(editPart);
	}
	
	public static void refreshVisuals(Diagram d, GraphicalEditPart editPart) {
		try {
			Rectangle bounds = new Rectangle(d.getPosition(),
					d.getSize());
			((GraphicalEditPart) editPart.getParent()).
					setLayoutConstraint(editPart, editPart.getFigure(), bounds);
		} catch (Exception e) {
			Debug.log_error(PosterboardPlugin.PLUGIN_ID, e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void refreshVisuals(WhiteboardDiagram d, GraphicalEditPart editPart) {
		Point location = d.getLocation();
		if (location == null) {
			location = getDefaultLocation();
		}
		Dimension size = d.getSize();
		if (size == null) {
			size = getDefaultSize();
		}
		Rectangle bounds = new Rectangle(location,
				size);
		((GraphicalEditPart) editPart.getParent()).
				setLayoutConstraint(editPart, editPart.getFigure(), bounds);
	}

	private static Dimension getDefaultSize() {
		return new Dimension(800, 800);
	}

	private static Point getDefaultLocation() {
		return new Point(40, 40);
	}

	public static boolean isPapyrusEditpart(EditPart part) {
		EditPart parent = part.getParent();
		if (parent instanceof PosterBoardEditpart) {
			//we dont care for diagram editparts, only children
			//of diagrams
			return false;
		}
		while (! (parent instanceof IPosterBoardDiagramEditpart)) {
			parent = parent.getParent();
		}
		if (parent instanceof AdaptedClassDiagramEditpart
				|| parent instanceof AdaptedUsecaseDiagramEditpart
				|| parent instanceof AdaptedSequenceDiagramEditpart
				|| parent instanceof AdaptedActivityDiagramEditpart
				|| parent instanceof AdaptedStateMachineDiagramEditpart) {
			return true;
		}
		return false;
	}
	
}
